//
//  TCUMutableBag.m
//  TreeMenuTest
//
//  Created by Etienne on 11/07/07.
//  Copyright (c) 2007 Etienne Samson. All rights reserved.
//

#import "TCUMutableBag.h"

static const void *MutableBagRetainCallBack (CFAllocatorRef allocator, const void *value) {
    return CFRetain(value);
}

static void MutableBagReleaseCallBack (CFAllocatorRef allocator, const void *value) {
    CFRelease(value);
}

@implementation TCUMutableBag
- (id) initWithCapacity:(int)capacity
{
    CFBagCallBacks callbacks;
    
    callbacks.version = 0;
    callbacks.retain = MutableBagRetainCallBack;
    callbacks.release = MutableBagReleaseCallBack;
    callbacks.copyDescription = CFCopyDescription;
    callbacks.equal = CFEqual;
    callbacks.hash = CFHash;
    
    self = (TCUMutableBag*)CFBagCreateMutable(kCFAllocatorDefault, capacity, &callbacks);
    return self;
}

- (void) addObject:(id)object
{
    if (!object)
        [NSException raise:NSInvalidArgumentException format:@"object cannot be nil"];
    
    CFBagAddValue((CFMutableBagRef)self, object);
}

- (void) removeObject:(id)object
{
    if (!object)
        [NSException raise:NSInvalidArgumentException format:@"object cannot be nil"];
    
    CFBagRemoveValue((CFMutableBagRef)self, (void*)object);
}

- (void) removeAllObjects
{
    CFBagRemoveAllValues((CFMutableBagRef)self);
}

@end
